package cn.flying.base.process.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import cn.flying.base.process.model.CfTaskRole;

/**
 * @description: 任务节点角色配置
 * @author: lvyong
 * @date: 2023年02月22日 00:18
 * @version: 1.0
 */
@Mapper
public interface ProcessTaskRoleMapper {

    /**
     * 保存
     *
     * @param taskRole
     */
    @Insert("insert into cf_task_role (id, task_id, task_name, task_type, role_id, role_group_id, proc_def_id, proc_def_key, tenant_id,revision,created_by,created_time,updated_by,updated_time,del_flag) " +
            "values (#{id}, #{taskId}, #{taskName}, #{taskType}, #{roleId}, #{roleGroupId}, #{procDefId}, #{procDefKey}, #{tenantId}, #{revision}, #{createdBy}, #{createdTime}, #{updatedBy}, #{updatedTime}, #{delFlag})")
    void insert(CfTaskRole taskRole);

    /**
     * 更新任务节点权限
     *
     * @param taskRole
     */
    @Update("update cf_task_role set role_id=#{roleId}, role_group_id=#{roleGroupId}, updated_by=#{updatedBy},updated_time=#{updatedTime} " +
            "where task_id = #{taskId} ")
    void updateTaskRole(CfTaskRole taskRole);

    /**
     * 查询任务的角色
     *
     * @param procDefId
     * @return
     */
    @Select("<script> " +
            "select id,task_id, task_name, task_type, role_id, role_group_id, proc_def_id, proc_def_key, tenant_id from cf_task_role where " +
            "<where>" +
            "<if test='procDefId != null and procDefId != \"\"'> and proc_def_id = #{procDefId} </if>" +
            "</where>" +
            "</script>")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "taskId", column = "task_id"),
            @Result(property = "taskName", column = "task_name"),
            @Result(property = "taskType", column = "task_type"),
            @Result(property = "roleId", column = "role_id"),
            @Result(property = "roleGroupId", column = "role_group_id"),
            @Result(property = "procDefId", column = "proc_def_id"),
            @Result(property = "procDefKey", column = "proc_def_key"),
            @Result(property = "tenantId", column = "tenant_id"),
    })
    List<CfTaskRole> getProcDefTaskRoles(String procDefId);


    /**
     * 查询任务的角色
     *
     * @param procDefId
     * @param taskId
     * @return
     */
    @Select("<script> " +
            "select id, task_name, task_type, role_id, role_group_id, proc_def_id, proc_def_key, tenant_id from cf_task_role where " +
            "<where>" +
            "<if test='procDefId != null and procDefId != \"\"'> and proc_def_id = #{procDefId} </if>" +
            "<if test='taskId != null and taskId != \"\"'> and task_id = #{taskId} </if>" +
            "</where>" +
            "</script>")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "taskId", column = "task_id"),
            @Result(property = "taskName", column = "task_name"),
            @Result(property = "taskType", column = "task_type"),
            @Result(property = "roleId", column = "role_id"),
            @Result(property = "roleGroupId", column = "role_group_id"),
            @Result(property = "procDefId", column = "proc_def_id"),
            @Result(property = "procDefKey", column = "proc_def_key"),
            @Result(property = "tenantId", column = "tenant_id"),
    })
    CfTaskRole getTaskRole(String procDefId, String taskId);
}

